package com.zhoug.zmui.navigation;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.zhoug.common.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

/**
 * 底部导航菜单组件容器
 *
 * @Author 35574
 * @Date 2020/7/20
 * @Description
 */
public class ZMUINavigationGroup extends LinearLayout {
    private static final String TAG = ">>>BottomNavigationGroup";
    private static final boolean DEBUG=false;
    /**
     * 选中的childView的下标
     */
    private int selectIndex = 0;
    private List<ZMUINavigationView> views = new ArrayList<>();
    private OnSelectedListener onSelectedListener;

    public ZMUINavigationGroup(Context context) {
        this(context, null);
    }

    public ZMUINavigationGroup(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZMUINavigationGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //水平布局
        setOrientation(LinearLayout.HORIZONTAL);
        //子view垂直居中
        setGravity(Gravity.CENTER_VERTICAL);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        init();
    }

    private void init() {
        views.clear();
        int childCount = getChildCount();
        if (childCount != 0) {
            for (int i = 0; i < childCount; i++) {
                ZMUINavigationView childView = (ZMUINavigationView) getChildAt(i);
                if (childView.isSelected()) {
                    selectIndex = i;
                }
                views.add(childView);
                childView.setTag(i);
                childView.setClickable(true);
                childView.setOnClickListener(onClickListener);
            }
        }
    }

    /**
     * 单击事件
     */
    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            synchronized (this) {
                int index = (int) v.getTag();
                if(DEBUG){
                    LogUtils.d(TAG, "onClick:"+index);
                }
                onSelectedChange(index);
            }
        }
    };

    private synchronized void onSelectedChange(int selected) {
        if(DEBUG){
            LogUtils.d(TAG, "onSelectedChange:"+selected);
        }
        int oldSelected = selectIndex;
        for (int i = 0; i < views.size(); i++) {
            ZMUINavigationView itemView = views.get(i);
            if (i == selected) {
                //设置选择
                if (itemView.isSelected()) {
                    oldSelected = selected;
                } else {
                    itemView.setSelected(true);
                }
            } else {
                //重置为不选中
                if (itemView.isSelected()) {
                    itemView.setSelected(false);
                    oldSelected = i;
                }
            }
        }
        selectIndex = selected;
        if (onSelectedListener != null) {
            onSelectedListener.onSelected(oldSelected, selected);
        }
    }

    public void setSelected(int selectIndex) {
        onSelectedChange(selectIndex);
    }

    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    /**
     * 选中监听
     */
    public interface OnSelectedListener {
        void onSelected(int oldSelected, int newSelected);
    }
}
